package com.yyon.mosaikan;

import org.anddev.andengine.entity.modifier.RotationAtModifier;
import org.anddev.andengine.entity.modifier.ScaleAtModifier;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

public class EnemySprite extends Sprite { // class keeps track of where enemy is on grid
	int pZ;
	int pR;
	TextureRegion region;
	private Mosaikan mosaikan;
	
	public EnemySprite(float pX, float pY, TextureRegion pTextureRegion, int pZ, int pR, Mosaikan mosaikan) {
		super(pX, pY, pTextureRegion);
		this.pZ = pZ; // get z-pos and rotation
		this.pR = pR;
		this.region = pTextureRegion;
		this.mosaikan = mosaikan; // get mosaikan handle
		// zoom enemy to correct z-position
        ScaleAtModifier scale = new ScaleAtModifier(0.001f, 1, (float) Math.pow(this.mosaikan.x, pZ), this.mosaikan.CAMERA_WIDTH/2-this.getX(), this.mosaikan.CAMERA_HEIGHT/2-this.getY());
        this.registerEntityModifier(scale);
        // rotate enemy to correct r-position
        RotationAtModifier rotate = new RotationAtModifier(0.001f, 0, 36*pR, (float) this.region.getWidth()/2, mosaikan.CAMERA_HEIGHT/2-this.getY());
        this.registerEntityModifier(rotate);
	}

	public void checkdeath () { // did the enemy die?
		if (this.pZ < 0) {
			this.kill(); // if it's gone off screen, die
		}
		if (this.pZ == 0 && this.pR == 0) {
			this.kill(); // if it was trapled, die
			this.mosaikan.score += 50; // and add 50 points!
			this.mosaikan.updatescore();
		}
	}
	
	private void kill() {
		this.mosaikan.killenemy(this); // kill this enemy (just calls mosaikan's enemy killing method)
	}

	public int getZ () { // getters and setters ...
		return pZ;
	}
	
	public int getR () {
		return pR;
	}
	
	public void setZ (int pZ) {
		this.pZ = pZ;
        ScaleAtModifier scale = new ScaleAtModifier(0.0001f, 1, (float) Math.pow(this.mosaikan.x, this.pZ), this.mosaikan.CAMERA_WIDTH/2-this.getX(), this.mosaikan.CAMERA_HEIGHT/2-this.getY());
        this.registerEntityModifier(scale);
	}
	
	public void setR (int pR) {
		pR = pR % 10;
		if (pR == 10) {
			pR = 0;
		} else if (pR == -1) {
			pR = 9;
		}
		this.pR = pR;
        RotationAtModifier rotate = new RotationAtModifier(0.0001f, 0, 36*pR, (float) this.region.getWidth()/2, this.mosaikan.CAMERA_HEIGHT/2-this.getY());
        this.registerEntityModifier(rotate);
	}
}
